package com.wang.util;

import cn.hutool.core.io.FileUtil;
import com.wang.constant.ErrorCode;
import com.wang.constant.FileUploadBizEnum;
import com.wang.exception.BusinessException;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class FileUploadUtils {
    /**
     * 校验文件
     *
     * @param multipartFile
     * @param fileUploadBizEnum 业务类型
     */
    public void validFile(MultipartFile multipartFile, FileUploadBizEnum fileUploadBizEnum) {
        // 文件大小
        long fileSize = multipartFile.getSize();
        // 文件后缀
        String fileSuffix = FileUtil.getSuffix(multipartFile.getOriginalFilename());
        final long ONE_M =10*10*1024 * 1024L;
        if (FileUploadBizEnum.USER_AVATAR.equals(fileUploadBizEnum)) {
            if (fileSize > ONE_M) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件大小不能超过 100M");
            }
            // 文件类型后缀 视频 文档 图片 音乐
            List<String> fileSuffixList = fileSuffixList(fileSuffix);
            if (!fileSuffixList.contains(fileSuffix)) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件类型错误");
            }
        }
    }
    /**
     * 校验文件类型是否是包含的文件
     *
     * @return
     */
    private List<String> fileSuffixList(String fileSuffix) {
        List<String> fileSuffixList = Arrays.asList("pdf", "doc", "docx", "ppt", "pptx","xls", "xlsx","mp3", "wav", "wma", "wax", "aac", "flac", "ogg", "wav", "wav","jpg", "jpeg", "png", "gif", "bmp", "webp", "tiff", "svg", "djvu", "xbm",
                "xpm", "xwd", "ico", "xbm", "xpm", "xwd", "xbm", "xpm", "xwd", "xbm", "xpm","xwd", "xbm", "xpm", "xwd",
                "mp4", "avi", "wmv", "flv", "mpeg", "mpg","txt"
        );
        if (!fileSuffixList.contains(fileSuffix)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件类型错误");
        }
        return fileSuffixList;
    }

    /**
     * 校验文件
     */
    public void validFile(MultipartFile file) {
        // 1. 文件大小校验
        long size = file.getSize();
        // 限制文件大小为10MB
        if (size >10*10 * 1024 * 1024) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件大小不能超过100MB");
        }

        // 2. 文件类型校验
        String contentType = file.getContentType();
        if (contentType == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件类型错误");
        }

        // 设置允许上传的文件类型
        Set<String> validTypes = new HashSet<>(Arrays.asList(
                // word文档
                "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                // excel文档
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                // powerpoint文档
                "application/vnd.ms-powerpoint",
                // pdf文档
                "application/pdf",
                "text/plain",
                "application/msword",
                "application/vnd.ms-excel",
                "application/vnd.ms-powerpoint",
                "application/vnd.openxmlformats-officedocument.presentationml.presentation",
                // 视频文件
                "video/mp4",
                "video/quicktime",
                "video/x-msvideo",
                "video/x-ms-wmv",
                "video/x-flv",
                "video/mpeg",
                "video/ogg",
                "video/webm",
                "video/x-matroska",
                "video/3gpp",
                "video/3gpp2",
                "video/x-ms-asf",
                "video/x-ms-wm",
                "video/x-ms-wmx",
                "video/x-ms-wvx",
                "video/x-ms-wmv",
                "video/x-ms-wmz",
                "video/x-ms-wmz",
                "video/x-ms-wmz",
                // 图片文件
                "image/jpeg",
                "image/png",
                "image/gif",
                "image/bmp",
                "image/webp",
                "image/tiff",
                "image/svg+xml",
                "image/vnd.djvu",
                "image/x-icon",
                "image/x-xbitmap",
                "image/x-xpixmap",
                "image/x-xwindowdump",
                // 音乐文件
                "audio/mpeg",
                "audio/x-wav",
                "audio/x-ms-wma",
                "audio/x-ms-wax",
                "audio/x-aac",
                "audio/x-flac",
                "audio/x-ogg",
                "audio/x-wav",
                "audio/x-wav"
        ));

        boolean b = !validTypes.contains(contentType) && !validFileType(file);
        if (b) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "不支持的文件类型");
        }
    }
    /**
     * 校验文件类型是否是pdf word文件
     *
     * @return
     */
    private boolean validFileType(MultipartFile multipartFile) {
        // 文件后缀
        String fileSuffix = FileUtil.getSuffix(multipartFile.getOriginalFilename());
        if (fileSuffix == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件类型错误");
        }
        // 文件类型后缀 视频 文档 图片 音乐
        fileSuffixList(fileSuffix);
        return true;
    }
}